package {{packageName}}.core.{{entity.module}}.service.impl;

{{#if entity.hasBigDecimal}}
import java.math.BigDecimal;
{{/if}}
import java.util.*;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
{{#if entity.clientEntityModel}}
import {{packageName}}.core.{{entity.module}}.client.{{entity.clientEntityModel.codeName}}FeignClient;
{{/if}};
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Page;
import org.springframework.util.*;
import cn.ibizlab.util.errors.*;
import cn.ibizlab.util.enums.CheckKeyStatus;
{{#system.enableGlobalTransaction}}
import io.seata.spring.annotation.GlobalTransactional;
{{/system.enableGlobalTransaction}}
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Lazy;
import {{packageName}}.core.{{entity.module}}.domain.{{entity.domainClassName}};
import {{packageName}}.core.{{entity.module}}.filter.{{entity.codeName}}SearchContext;
import {{packageName}}.core.{{entity.module}}.service.{{entity.codeName}}Service;
import com.alibaba.fastjson.JSONObject;

/**
 * 实体[{{entity.logicName}}] 服务对象接口实现
 *
 * @author generator
 */
@Slf4j
public abstract class Abstract{{entity.codeName}}Service implements {{entity.codeName}}Service {

    {{#if entity.clientEntityModel}}
    @Autowired
    {{entity.clientEntityModel.codeName}}FeignClient {{camelCase entity.clientEntityModel.codeName}}FeignClient;
    {{/if}}

    protected int batchSize = 500;

{{#each entity.implementMethods}}
    {{#if dataSet}}
     public {{outParam}} fetch{{pascalCase codeName}}({{entity.codeName}}SearchContext context) {
        {{#if dataSet.sortInfo}}
        if(context.getPageSort() == null || context.getPageSort() == Sort.unsorted())
            context.setSort("{{dataSet.sortInfo}}");
        {{/if}}
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.fetch{{pascalCase codeName}}(context);
    }
	
    public List<{{entity.domainClassName}}> list{{pascalCase codeName}}({{entity.codeName}}SearchContext context) {
        context.setSize(Short.MAX_VALUE);
        return fetch{{pascalCase codeName}}(context).getContent();
    }
	
    {{else if builtinAction}}
        {{#if (eq codeName 'Get')}}
    public {{outParam}} {{camelCase codeName}}({{entity.domainClassName}} et) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.getBy{{pascalCase entity.keyField.codeName}}(et.get{{pascalCase entity.keyField.codeName}}());
    }	

    public List<{{outParam}}> {{camelCase codeName}}(List<{{entity.domainClassName}}> entities) {
        return null;
    }	
	
        {{else if (eq codeName 'GetDraft')}}
    public {{outParam}} {{camelCase codeName}}({{inParam}} {{inParamName}}) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.getDraft(et);
    }
	
        {{else if (eq codeName 'CheckKey')}}
    public CheckKeyStatus {{camelCase codeName}}({{inParam}} et) {
         return {{camelCase entity.clientEntityModel.codeName}}FeignClient.checkKey(et);
    }
	
        {{else if (eq codeName 'Create')}}
    @Override
    {{#eq entity.actions.create.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.create.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public {{outParam}} {{camelCase codeName}}({{inParam}} et) {
        {{entity.domainClassName}} rt = {{camelCase entity.clientEntityModel.codeName}}FeignClient.create(et);
        rt.copyTo(et,true);
        return true;
    }
	
    {{#eq entity.actions.create.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.create.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public {{outParam}} {{camelCase codeName}}(List<{{inParam}}> list) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.createBatch(list);
    }
	
        {{else if (eq codeName 'Update')}}
    {{#eq entity.actions.update.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.update.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public {{outParam}} {{camelCase codeName}}({{inParam}} et) {
        {{entity.domainClassName}} rt = {{camelCase entity.clientEntityModel.codeName}}FeignClient.updateBy{{pascalCase entity.keyField.codeName}}(et.get{{pascalCase entity.keyField.codeName}}(), et);
        rt.copyTo(et,true);
        return true;
    }

    {{#eq entity.actions.update.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.update.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public {{outParam}} {{camelCase codeName}}(List<{{inParam}}> list) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.updateBatch(list);
    }
	
        {{else if (eq codeName 'Save')}}
    @Override
    {{#eq entity.actions.save.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.save.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public {{outParam}} {{camelCase codeName}}({{inParam}} et) {
       {{entity.domainClassName}} rt =  {{camelCase entity.clientEntityModel.codeName}}FeignClient.save(et);
        rt.copyTo(et,true);
        return true;
    }

    {{#eq entity.actions.save.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.save.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public {{outParam}} {{camelCase codeName}}(List<{{inParam}}> list) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.saveBatch(list);
    }
	
        {{else if (eq codeName 'Remove')}}
   {{#eq entity.actions.remove.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.remove.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public {{outParam}} {{camelCase codeName}}({{entity.domainClassName}} et) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.removeBy{{pascalCase entity.keyField.codeName}}(et.get{{pascalCase entity.keyField.codeName}}());
    }

    {{#eq entity.actions.remove.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.remove.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public {{outParam}} {{camelCase codeName}}(List<{{entity.domainClassName}}> entities) {
       return {{camelCase entity.clientEntityModel.codeName}}FeignClient.removeBatch(entities.stream().map(e->e.get{{pascalCase entity.keyField.codeName}}()).collect(Collectors.toList()));
    }		

        {{/if}}
    {{/if}}
{{/each}}

}